//
//  WBWelcome.swift
//  Weibo
//
//  Created by apple on 2016/12/10.
//  Copyright © 2016年 yanjun. All rights reserved.
//

import UIKit
import SDWebImage

class WBWelcome: UIView {

    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var tipLabel: UILabel!
    @IBOutlet weak var bottomcons: NSLayoutConstraint!
    class func welcomeView() -> WBWelcome {
        
        let nib = UINib(nibName: "WBWelcome", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcome
        
        v.frame = UIScreen.main.bounds
        
        return v 
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        // 提示：initWtihCode 只是刚刚从 Xib 的二进制文件将视图数据加载完成
        // 还没有和代码连线建立关系，开发时，千万不要在这个方法分钟处理 UI
        print("coder+ \(iconView)")
    }
    
    override func awakeFromNib() {
        
        
        guard let urlString = WBNetworkManager.shared().userAccount.avatar_large else {
            return
        }
        
        let url = URL(string: urlString)
        
        iconView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar"));
        
        
    }
    
    
    
    // 自动布局系统更新完成约束后，会自动调用此方法
    // 通常是对子视图布局进行修改
//    override func layoutSubviews() {
//        
//    }
    
    // 视图被添加到 window 上，表示视图已经显示
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        self.layoutIfNeeded()
        
        bottomcons.constant = bounds.size.height - 200
        
        UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
        
            // 更新约束
            self.layoutIfNeeded()
            
            
        }, completion: { (_) in
        
            UIView.animate(withDuration: 1.0, animations: { 
                
                self.tipLabel.alpha = 1
                
            }, completion: { (_) in
                
                self.removeFromSuperview()
                
            })
        
        })
        
    }
    

}
